<?php

class Model_perjalanan extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->helper('text');
    }

    function get_pengemudi() {  
        $now = date("Y-m-d H:i:s");      
        $sql = "SELECT trip.source_label, trip.destination_label, user.name, trip.departTime,
                        trip.via, trip.priceInRupiah, trip.priceInPoint, trip.id as tripId, user.id,
                        trip.status
                from trip,user
                where user.id = trip.userId and status = 'P' order by departTime desc limit 5";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $value) {
                $a[] = $value;
            }
            return $a;
        } else {
            return FALSE;
        }
    }

    function get_penumpang() {  
        $now = date("Y-m-d H:i:s");      
        $sql = "SELECT trip.source_label, trip.destination_label, user.name, trip.departTime,
                        trip.via, trip.priceInRupiah, trip.priceInPoint, trip.id as tripId, user.id,
                        trip.status
                from trip,user
                where user.id = trip.userId and status = 'U' order by departTime desc limit 5";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $value) {
                $a[] = $value;
            }
            return $a;
        } else {
            return FALSE;
        }
    }

    function get(){
        //cek session
        $isLogin = $this->session->userdata('isLogin');
        $user = ''; $id='';
        if ($isLogin) {
            $user = $this->session->userdata('username');
            $id = $this->session->userdata('user_id');
        }

        if($this->input->post('radius')){
            $myid = $id;
            $radius = $this->input->post('radius');
            $mylat = 0;
            $mylong = 0;
            $query_coor = $this->db->query("SELECT latitude,longitude FROM coordinate WHERE idUser=".$myid);
            $row_coor = $query_coor->row(); 
            if($query_coor ->num_rows() > 0){
                $mylat = $row_coor->latitude;
                $mylong = $row_coor->longitude;
            }
        
            $query = $this->db->query("SELECT t.*, u.`name`,  u.`avatar`, u.lastUpdatedAvatar, u.isPremium, u.gender, c.latitude, c.longitude FROM trip t JOIN `user` u ON u.id=t.userId JOIN coordinate c ON c.idUser=u.id WHERE  t.is_available=1 AND departTime> '".date("Y-m-d H:i:s")."' ORDER BY departTime DESC");
            if($query->num_rows() < 1 || $mylat == 0|| $mylong == 0){
                $data_output['timeline'] = "";
            }else{
                $row = $query->row(); 
                $count = 0;
                foreach($query->result() as $row){
                    $distance = $this->distance($mylat,$mylong,$row->latitude,$row->longitude);
                    if( $distance <= $radius){
                        $data_output["timeline"][$count]["id"] = $row->id;
                        $data_output["timeline"][$count]["userId"] = $row->userId;
                        $data_output["timeline"][$count]["name"] = $row->name;
                        $data_output["timeline"][$count]["avatar"] = $row->avatar;
                        $data_output["timeline"][$count]["lastUpdatedAvatar"] = $row->lastUpdatedAvatar;
                        $data_output["timeline"][$count]["gender"] = $row->gender;
                        $data_output["timeline"][$count]["isPremium"] = $row->isPremium;
                        $data_output["timeline"][$count]["status"] = $row->status;
                        $data_output["timeline"][$count]["source_label"] = $row->source_label;
                        $data_output["timeline"][$count]["source_latitude"] = $row->source_latitude;
                        $data_output["timeline"][$count]["source_longitude"] = $row->source_longitude;
                        $data_output["timeline"][$count]["destination_label"] = $row->destination_label;
                        $data_output["timeline"][$count]["destination_latitude"] = $row->destination_latitude;
                        $data_output["timeline"][$count]["destination_longitude"] = $row->destination_longitude;
                        $data_output["timeline"][$count]["via"] = $row->via;
                        $data_output["timeline"][$count]["priceInRupiah"] = $row->priceInRupiah;
                        $data_output["timeline"][$count]["priceInPoint"] = $row->priceInPoint;
                        $data_output["timeline"][$count]["departTime"] = $row->departTime;
                        $data_output["timeline"][$count]["notes"] = $row->notes;
                        $count++;
                    }
                }   
                if($count == 0)             $data_output['timeline'] = "";
            }
        }else{
            $query = $this->db->query("SELECT t.*, u.`name`,  u.`avatar`, u.lastUpdatedAvatar, u.isPremium, u.gender, c.latitude, c.longitude FROM trip t JOIN `user` u ON u.id=t.userId JOIN coordinate c ON c.idUser=u.id WHERE  t.is_available=1 AND departTime> '".date("Y-m-d H:i:s")."' ORDER BY departTime DESC LIMIT 20");
            if($query->num_rows() < 1){
                $data_output['timeline'] = "";
            }else{
                $row = $query->row(); 
                $count = 0;
                foreach($query->result() as $row){
                    $data_output["timeline"][$count]["id"] = $row->id;
                    $data_output["timeline"][$count]["userId"] = $row->userId;
                    $data_output["timeline"][$count]["name"] = $row->name;
                    $data_output["timeline"][$count]["avatar"] = $row->avatar;
                    $data_output["timeline"][$count]["lastUpdatedAvatar"] = $row->lastUpdatedAvatar;
                    $data_output["timeline"][$count]["gender"] = $row->gender;
                    $data_output["timeline"][$count]["isPremium"] = $row->isPremium;
                    $data_output["timeline"][$count]["status"] = $row->status;
                    $data_output["timeline"][$count]["source_label"] = $row->source_label;
                    $data_output["timeline"][$count]["source_latitude"] = $row->source_latitude;
                    $data_output["timeline"][$count]["source_longitude"] = $row->source_longitude;
                    $data_output["timeline"][$count]["destination_label"] = $row->destination_label;
                    $data_output["timeline"][$count]["destination_latitude"] = $row->destination_latitude;
                    $data_output["timeline"][$count]["destination_longitude"] = $row->destination_longitude;
                    $data_output["timeline"][$count]["via"] = $row->via;
                    $data_output["timeline"][$count]["priceInRupiah"] = $row->priceInRupiah;
                    $data_output["timeline"][$count]["priceInPoint"] = $row->priceInPoint;
                    $data_output["timeline"][$count]["departTime"] = $row->departTime;
                    $data_output["timeline"][$count]["notes"] = $row->notes;
                    $count++;
                }   
                if($count == 0)             $data_output['timeline'] = "";
            }
        }
        return $data_output;
    }

}

?>